//
//  WatermarkView.m
//  自定义相册
//
//  Created by DongSen on 2018/7/9.
//  Copyright © 2018年 beok. All rights reserved.
//

#import "Masonry.h"
#import "WatermarkView.h"

@implementation WatermarkView
-(void)setUpUI{
    self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    [self.logoImage mas_makeConstraints:^(MASConstraintMaker *make){
        make.bottom.equalTo(self.mas_bottom).with.offset(0);
        make.right.equalTo(self.mas_right).with.offset(-10);
        make.height.mas_equalTo(60); // 注意1：Height == 绿色View（它推断出Height）
        make.width.mas_equalTo(60); // 注意1：Height == 绿色View（它推断出Height）
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make){
        make.bottom.equalTo(self.mas_bottom).with.offset(-45);
        make.right.equalTo(self.logoImage.mas_left).with.offset(-10);
        make.height.mas_equalTo(15); // 注意1：Height == 绿色View（它推断出Height）
    }];

    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.equalTo(self.nameLabel.mas_bottom).with.offset(0);
        make.right.equalTo(self.logoImage.mas_left).with.offset(-10);
        make.height.mas_equalTo(15); // 注意1：Height == 绿色View（它推断出Height）
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.equalTo(self.numberLabel.mas_bottom).with.offset(0);
        make.right.equalTo(self.logoImage.mas_left).with.offset(-10);
        make.height.mas_equalTo(15); // 注意1：Height == 绿色View（它推断出Height）
    }];
    [self.locationLabel mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.equalTo(self.timeLabel.mas_bottom).with.offset(0);
        make.right.equalTo(self.logoImage.mas_left).with.offset(-10);
        make.height.mas_equalTo(15); // 注意1：Height == 绿色View（它推断出Height）
    }];
    [self.locationImage mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.equalTo(self.timeLabel.mas_bottom).with.offset(0);
        make.right.equalTo(self.locationLabel.mas_left).with.offset(0);
        make.height.mas_equalTo(15); // 注意1：Height == 绿色View（它推断出Height）
        make.width.mas_equalTo(15); // 注意1：Height == 绿色View（它推断出Height）
    }];
}
- (UIImageView *)logoImage{
    if (!_logoImage) {
        _logoImage = [[UIImageView alloc] init];
        _logoImage.backgroundColor = [UIColor yellowColor];
        [self addSubview:_logoImage];
    }
    return _logoImage;
}
- (UIImageView *)locationImage{
    if (!_locationImage) {
        _locationImage = [[UIImageView alloc] init];
        _locationImage.backgroundColor = [UIColor greenColor];
        [self addSubview:_locationImage];
    }
    return _locationImage;
}
- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:_nameLabel];
    }
    return _nameLabel;
}
- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:_timeLabel];
    }
    return _timeLabel;
}
- (UILabel *)numberLabel{
    if (!_numberLabel) {
        _numberLabel = [[UILabel alloc] init];
        _numberLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:_numberLabel];
    }
    return _numberLabel;
}
- (UILabel *)locationLabel{
    if (!_locationLabel) {
        _locationLabel = [[UILabel alloc] init];
        _locationLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:_locationLabel];
    }
    return _locationLabel;
}
- (UIImage *)screenImage{
    CGFloat oldAlpha = self.alpha;
    UIColor * color = self.backgroundColor;
    self.backgroundColor = [UIColor clearColor];
    UIColor * namecolor = self.nameLabel.textColor;
    self.nameLabel.textColor = [UIColor whiteColor];
    self.timeLabel.textColor = [UIColor whiteColor];
    self.timeLabel.text = [self getData];
    self.numberLabel.textColor = [UIColor whiteColor];
    self.locationLabel.textColor = [UIColor whiteColor];
    self.alpha = 1;
    UIGraphicsBeginImageContextWithOptions(CGSizeMake([UIScreen mainScreen].bounds.size.width, self.bounds.size.height), NO, 0);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.alpha = oldAlpha;
    self.backgroundColor = color;
    self.nameLabel.textColor = namecolor;
    self.timeLabel.textColor = namecolor;
    self.numberLabel.textColor = namecolor;
    self.locationLabel.textColor = namecolor;
    return resultingImage;
}
-(NSString*)getUUIDs{
    return [NSString stringWithFormat:@"%@/%ld",[UIDevice currentDevice].identifierForVendor,(long)[[NSDate date] timeIntervalSince1970]];
}
- (NSString*)getData{
    NSDateComponents *dateComponents = [self dateComponents];
    NSString * date = [NSString stringWithFormat:@"%ld-%ld-%ld", (long)dateComponents.year, (long)dateComponents.month, (long)dateComponents.day];
    NSString *time = [NSString stringWithFormat:@"%ld:%02ld", (long)dateComponents.hour, (long)dateComponents.minute];
    return [NSString stringWithFormat:@"%@ %@", date, time];
}
- (UIImage *)imageByRenderingView{
    CGFloat oldAlpha = self.alpha;
    self.alpha = 1;
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.alpha = oldAlpha;
    return resultingImage;
}
- (NSDateComponents *)dateComponents
{
    NSCalendarUnit calendarUnit =
    NSEraCalendarUnit
    | NSYearCalendarUnit
    | NSMonthCalendarUnit
    | NSDayCalendarUnit
    | NSHourCalendarUnit
    | NSMinuteCalendarUnit
    | NSSecondCalendarUnit
    | NSWeekCalendarUnit
    | NSWeekdayCalendarUnit
    | NSWeekdayOrdinalCalendarUnit
    | NSQuarterCalendarUnit
    | NSWeekOfMonthCalendarUnit
    | NSWeekOfYearCalendarUnit
    | NSYearForWeekOfYearCalendarUnit
    | NSCalendarCalendarUnit
    | NSTimeZoneCalendarUnit;
    return [[NSCalendar currentCalendar] components:calendarUnit fromDate:[NSDate date]];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
